package com.tablefood.dao.Impl;

import java.util.List;

import javax.annotation.Resource;

import org.hibernate.Query;
import org.hibernate.SessionFactory;

import com.tablefood.dao.CartDao;
import com.tablefood.domain.ShopCart;
import com.tablefood.domain.ShopUser;

public class CartDaoImpl implements CartDao{

	@Resource
	private SessionFactory sessionFactory; 
	/*
	 * 生成一个新的购物车
	 * */
	@Override
	public ShopCart insertCart(ShopCart cart) {
		System.out.println("生成一个购物车");
		sessionFactory.getCurrentSession().save(cart); 
		System.out.println("生成成功");
		return cart;
	}

	/*
	 * 通过userid查询购物车所有的商品
	 * */
	@Override
	public List searchCartByUserid(String userid) {

		String sql = "from ShopCart as cart where cart.shopUser.userid=?";
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setString(0, userid);
		if(query.list().size()!=0){
			System.out.println("有结果："+query.list().size());
			List<ShopCart> result = query.list();
			return result;
		}else{
			System.out.println("没有结果");
			return null ;
		}
		
	}

	@Override
	public List searchCartAboutProduct(String userid, String productid) {
		
		String sql = "from ShopCart as cart where cart.shopUser.userid=?and cart.shopProduct.productid=?";
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setString(0, userid);
		query.setString(1, productid);
		if(query.list().size()!=0){
			System.out.println("有结果："+query.list().size());
			List<ShopCart> result = query.list();
			return result;
		}else{
			System.out.println("没有结果");
			return null ;
		}
		 
	}

	@Override
	public ShopCart updateCart(ShopCart cart) {
		sessionFactory.getCurrentSession().update(cart); 
		return cart;
	}

	//结算后，删除该用户的所有的购物车
	@Override
	public void delCart(String userid) {
		System.out.println("删除购物车");
		String sql = "delete from ShopCart as cart where cart.shopUser.userid=";
        sessionFactory.getCurrentSession().createQuery(sql+userid).executeUpdate();
	}

	@Override
	public ShopCart searchOneCartByCartId(String cartid) {
		String sql = "from ShopCart as cart where cart.cartid=?";
		Query query = sessionFactory.getCurrentSession().createQuery(sql); 
		query.setString(0, cartid);
		if(query.list().size()>0){
			List<ShopCart> list = query.list();
			ShopCart cart = list.get(0);
			return cart;
		}else
			return null;
	}

	@Override
	public boolean delOneCart(Long cartid) {
		
		try{
			String sql = "delete from ShopCart as cart where cart.cartid=";
	        sessionFactory.getCurrentSession().createQuery(sql+cartid).executeUpdate();
	        return true;
        }catch(Exception e){
        	e.printStackTrace();
        	return false;
        }
		
	}

	@Override
	public List<ShopCart> queryForPage(String hql, String userid,int offset, int length) {
		
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, userid);
		query.setFirstResult(offset);
		query.setMaxResults(length);
		List<ShopCart> list = query.list();
		
		return list;
	}

	@Override
	public int getAllRowCount(String hql,String userid) {
		
		Query query = sessionFactory.getCurrentSession().createQuery(hql);
		query.setString(0, userid);
		return query.list().size();
	}

}
